package com.polly.esphwevent.filter;

import com.polly.esphwevent.common.constants.AuthConstants;
import com.polly.esphwevent.services.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liqin@nd.com.cn
 * @version V1.0
 * @title: AuthenticationFilter
 * @description: TODO(用一句话描述该文件做什么)
 * @date: 2019/2/22 17:43
 */

public class AuthenticationFilter implements HandlerInterceptor {


    @Autowired
    CommentService commentService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String requestURI = null;
        if (httpServletRequest != null) {
            //获取context-path加servlet-path
            requestURI = httpServletRequest.getRequestURI();
        }

        //auth打头的不进行校验
        if (requestURI.startsWith("/auth")) {
            return true;
        }
        if(commentService.doValidate(httpServletRequest)){
            //put
            httpServletRequest.setAttribute(AuthConstants.USER_ID, 1);
            httpServletRequest.setAttribute(AuthConstants.USER_ORG_ID, 2l);

            return true;
        }
        else
            return false;


    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
